<?php
/**
 * Created by PhpStorm.
 * User: zhenl
 * Date: 2022/12/7
 * Time: 23:27
 */

namespace App\Channel;
use Illuminate\Support\Facades\Log;

class Russia
{

    //用户充值功能
    public function chargeforuser($amount, $thirdOrderNumber, $paymentOption)
    {
        //充值接口，这个接口post方式以json格式提交
        $url = 'https://api.eggoout.com/payin/unifiedorder.do';
        $apikey = 'b840cebc620145a58b2dbb6ea1b035a0';
        $userid = 'M230207190113001783';
        if($paymentOption == 'EUR'){
            $channelCode = 'OUYUAN808';
        }elseif ($paymentOption == 'USD') {
            $channelCode = 'MG101';
        }
        
        $data = [
            'merchantNo' => $userid,
            'merchantOrderId' => $thirdOrderNumber,
            'channelCode' => $channelCode,
            'currency' => $paymentOption,
            'email' => 'shop'.rand(1000000,99999999),
            'userName' => 'Mr',
            'mobileNo' => rand(1000000,99999999),
            'amount' => $amount*100,
            'expireTime' => 710,
            'notifyUrl' => 'https://uggmall.net/pay-notify/russia',//
        ];
        $sign = $this->mdsign($apikey,$data);
        $data['version'] = '1.0';
        $data['subject'] = 'shopping';
        $data['sign'] = $sign;

        //向充值接口发起充值请求 post方式以json格式提交
        $ret = $this->curlhead($url, $data, 'POST');
        Log::error('51Pay支付参数：' . json_encode($data));
        //获取返回的订单信息json字符串
        $response = json_decode($ret, true);
        
        if($response['code'] == '000') {
            return $response['data']['checkStand'];
            
        }
        return flash($response['msg'])->error();
    }


    public function curlhead($url, $params, $m)
    {
        $curl = curl_init();
        $data_string = json_encode($params);
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POSTFIELDS => $data_string,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
            ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    public function mdsign($apikey,$native){
        ksort($native);
        $md5str = "";
        foreach ($native as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }
        $md5str = $md5str.'key='.$apikey;
        $sign = hash_hmac('sha256', $md5str, $apikey);
        return $sign;
    }
}